package com.wsz.security.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.EnableGlobalAuthentication;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

import java.util.HashMap;
import java.util.Map;


@Configuration
@EnableGlobalMethodSecurity(prePostEnabled=true,securedEnabled=true, jsr250Enabled=true)
public class WebConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public UserDetailsService userDetailsService(){
        InMemoryUserDetailsManager inMemoryUserDetailsManager = new InMemoryUserDetailsManager();
        inMemoryUserDetailsManager.createUser(User.withUsername("user").password("{noop}123").roles("admin").build());
        inMemoryUserDetailsManager.createUser(User.withUsername("wsz").password("{noop}123").roles("root").build());
        inMemoryUserDetailsManager.createUser(User.withUsername("xs").password("{noop}123").authorities("READ_info").build());
        return inMemoryUserDetailsManager;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService());
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .mvcMatchers("/admin/**").hasAnyRole("root","admin")
                .mvcMatchers("/root/**").hasRole("root")
                .mvcMatchers("/info/**").hasAuthority("READ_info")
                .anyRequest().authenticated()
                .and()
                .formLogin()
                .and()
                .exceptionHandling()
//                .authenticationEntryPoint(((request, response, e) -> {
//                    response.setContentType("application/json;charset=UTF-8");
//                    Map<String,Object> data = new HashMap<>();
//                    data.put("msg:","请登录后在进行操作");
//                    data.put("status:",201);
//                    String s = new ObjectMapper().writeValueAsString(data);
//                    response.getWriter().println(s);
//                }))
                .accessDeniedHandler((request, response, e) -> {
                    response.setContentType("application/json;charset=UTF-8");
                    Map<String,Object> data = new HashMap<>();
                    data.put("msg:","您没有该权限！");
                    data.put("status:",201);
                    String s = new ObjectMapper().writeValueAsString(data);
                    response.getWriter().println(s);
                })
                .and()
                .csrf().disable();
    }
}
